package com.ydd.utils;

import com.ydd.constant.JwtClaimsConstant;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public  class CommonUntil {

    /**
     *     将数据库中["XXXX"] 数据格式转换为 字符串数组
     */
    public static String[] String2Array(String str){
        String substring = str.substring(1, str.length()-1);
        String replace = substring.replace("\"", "");
        String[] strArr = replace.split(",");
        return strArr;
    }

    /**
     * 将字符串数组 格式转换为 数据库中["xxxx","xxx",...]格式
     * @param strArr
     * @return
     */
    public static String Array2String(String[] strArr){
        String roleItem = "";
        String start = "[\"";
        String end = "\"]";
        for (int i = 0; i < strArr.length; i++) {
            if (i == strArr.length-1){
                roleItem += strArr[i] + end;
            } else {
                roleItem += strArr[i] + ",";
            }
        }
        String roleIds = start + roleItem;
        return roleIds;
    }


    /**
     * 去除字符串的[""]
     * @param str
     * @return
     */
    public static String StringToCom(String str){
        String substring = str.substring(1, str.length()-1);
        String replace = substring.replace("\"", "");
        return replace;
    }


    public static String getToken(Integer id,String secretKey,Long tel){
        Map<String, Object> claims = new HashMap<>();// 定义载荷
        claims.put(JwtClaimsConstant.USER_ID, id); // 将登录成功的id传入令牌载荷
        // 生成令牌
        String token = JwtUtil.createJWT(secretKey,tel,claims);
        return token;
    }

    /**
     * 将localDatTime转换为long的时间毫秒值
     * @param localDateTime
     * @return
     */
    public static long locaDataTime2Long(LocalDateTime localDateTime){
        long time = Timestamp.valueOf(localDateTime).getTime();
        return time;
    }
}
